package com.face.server.controller;

import com.face.RespMsg;
import com.face.server.entity.Student;
import com.face.server.repository.StudentRepository;
import com.face.server.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created by ryan on 2018/1/25.
 */
//@RestController
//@RequestMapping("/student")
@Deprecated
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/findOne/{sid}")
    public RespMsg findOne(@PathVariable String sid){
        if (sid==null||sid.equals("")) {
            return RespMsg.error(1, "id不能为空");
        }
        Student student=new Student();
        student=studentRepository.findOne(sid);
        student.setAuth(null);
        return RespMsg.build(student);
    }

    @PostMapping("/editInfo/{sid}")
    public RespMsg editInfo(@PathVariable String sid, @RequestParam("proPhoto")MultipartFile proPhoto,@RequestParam String userName,@RequestParam String phone) throws IOException {
        if (proPhoto == null || proPhoto.isEmpty()) {
            return RespMsg.error(1, "文件不能为空");
        }
        return RespMsg.build(studentService.editInfo(sid,proPhoto,userName,phone));
    }


    @PostMapping("/addStudent")
    public RespMsg addStudent(@RequestParam String stuName,@RequestParam Boolean stuSex,@RequestParam Integer stuNum,@RequestParam String idNum,
                              @RequestParam Integer buildNum,@RequestParam Integer dormNum,@RequestParam String className,@RequestParam String stuPhone){
        //学号 身份证长度要求 是否要验证学生已在寝室中
        return RespMsg.build(studentService.addStudent(stuName,stuSex,stuNum,idNum,buildNum,dormNum,className,stuPhone));
    }


    @GetMapping(value = {"/search/{size}/{page}"})
    public RespMsg listStudents(@PathVariable Integer size, @PathVariable Integer page, @RequestParam(value = "key1",required = false) String key1,@RequestParam(value = "key2",required = false) String key2){
        return RespMsg.build(studentService.listStudents(new PageRequest(page,size, Sort.Direction.ASC,"dormitoryBuildNum"),key1,key2));
    }

    @DeleteMapping(value = "/deleteStudents")
    public RespMsg deleteStudents(@RequestBody(required = false)String sids){
        return RespMsg.build(studentService.deleteStudents(sids));
    }

    @GetMapping("/findByUsername/{username}")
    public RespMsg findByUsername(@PathVariable String username){
        if (username==null||username.equals("")) {
            return RespMsg.error(1, "username不能为空");
        }
        Student student=new Student();
        student=studentRepository.findByUsername(username);
        student.setAuth(null);
        return RespMsg.build(student);
    }



}
